const qs = require('qs');
const Promise = require('bluebird');
const axios = require('axios');

/**
 * 获取url参数
 */
const getQueryString = function (url, name) {
    var reg = new RegExp("[\?|\&]" + name + "=([^&]*)(&|$)", "i");
    var r = url.match(reg);
    if (r != null) return r[1];
    return null;
}

const getRealUrl = function (url) {
    return Promise.resolve().then(function () {
        if (url.indexOf('item.taobao.com/item.htm') != -1 || url.indexOf('h5.m.taobao.com/awp/core/detail.htm') != -1 || url.indexOf('detail.tmall.com/item.htm') != -1 || url.indexOf('detail.m.tmall.com/item.htm') != -1) {
            return url.split('?')[0] + '?id=' + getQueryString(url, 'id');
        }
        if (url.indexOf("a.m.taobao.com/i") != -1) {
            let arr = url.match(/\:\/\/a\.m\.taobao\.com\/i(.*?)\.htm/i);
            if (arr && arr[1]) {
                return 'https://item.taobao.com/item.htm?id=' + arr[1];
            }
        }
        if (url.indexOf("uland.taobao.com/coupon/edetail") != -1) {
            let itemId = getQueryString(url, 'itemId');
            if (itemId) {
                return 'https://item.taobao.com/item.htm?id=' + itemId;
            }
        }
        return axios.get(url).then(function (response) {
            let content = response.data;
            if (content.indexOf("var url = 'https://item.taobao.com/item.htm?") != -1 || content.indexOf("var url = 'http://item.taobao.com/item.htm?") != -1) {
                let arr = content.match(/var url \= \'((https|http)\:\/\/item\.taobao\.com\/item\.htm\?.*?)\';/i);
                if (arr && arr[1]) {
                    return 'https://item.taobao.com/item.htm?id=' + getQueryString(arr[1], 'id');
                }
            } else if (content.indexOf("var url \= \'https\:\/\/a.m.taobao.com\/i") != -1) {
                let arr = content.match(/var url \= \'https\:\/\/a\.m\.taobao\.com\/i(.*?)\.htm\?.*?\';/i);
                if (arr && arr[1]) {
                    return 'https://item.taobao.com/item.htm?id=' + arr[1];
                }
            } else if (content.indexOf('"bizId":') != -1) {
                let arr = content.match(/"bizId"\s*:\s*"(\d*)"/i);
                if (arr && arr[1]) {
                    return 'https://detail.tmall.com/item.htm?id=' + arr[1];
                }
            }
            let url = response.request._redirectable && response.request._redirectable._isRedirect && response.request._redirectable._currentUrl;
            if (url && (url.indexOf('item.taobao.com/item.htm') != -1 || url.indexOf('detail.tmall.com/item.htm') != -1)) {
                return url.split('?')[0] + '?id=' + getQueryString(url, 'id');
            }
            return '';
        });
    });
}

let url = 'http://v.cvz5.com/h.FPhgco';
// let url = 'https://a.m.taobao.com/i526377126870.htm?price=38-62&sourceType=item&sourceType=item&suid=50b43d4a-e6a9-4ca6-9619-2fd7626d11c1&ut_sk=1.VgkoYhtJoqMDACxpkMCEJOB8_21646297_1504480598887.TaoPassword-WeiXin.1&un=985a6e0a94b43e343dd3af49e2902fce&share_crt_v=1';
// let url = 'https://market.m.taobao.com/apps/crowd/crowd-abs/dashenDetail.html?groupId=80&contentId=200376857629&outer=true&sourceType=other&suid=9faa519a-8024-46f1-bf51-bf79bb43e129&ut_sk=1.VoVRBJmc3SkDAJy3bKotlbxJ_21646297_1508026777670.TaoPassword-WeiXin.10000&un=1579e7c97d3e1362875dbdeffcd1c5a5&share_crt_v=1';
getRealUrl(url).then(function (realUrl) {
    console.log(realUrl);
});